package com.dongbao.dialog;

import com.dongbao.core.config.FlowChartState;
import com.dongbao.util.StorageConfig;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import lombok.Getter;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class SettingsForm extends SimpleToolWindowPanel {

    @Getter
    private JPanel rootPanel;

    @Getter
    private JTextField testValue;

    /**
     * 是否有修改的内容
     * */
    public Boolean isModified = false;


    public SettingsForm(boolean vertical) {
        super(vertical);
    }

    public SettingsForm(boolean vertical, boolean borderless) {
        super(vertical, borderless);
    }

    public SettingsForm() {
        super(true, true);
        add(rootPanel);
        /* 从存储中将值取出还原 */
        StorageConfig config = FlowChartState.getInstance().getConfig();
        if (null == config) {
            config = new StorageConfig();
        }
        testValue.setText(config.getValue());

        /* 监听事件 */
        testValue.getDocument().addDocumentListener(new DocumentListener(){
            public void changedUpdate(DocumentEvent e){
                // 当attribute改变时触发
                // 有待继续探索......
                isModified = true;
            }
            public void removeUpdate(DocumentEvent e){
                // 当textField内容被删减时触发
                // 对应到用户行为上可以是用户按下退格键
                isModified = true;
            }
            public void insertUpdate(DocumentEvent e){
                // 当textField内容增加时触发
                // 对应到用户行为是用户键入内容
                isModified = true;
            }
        });
    }

    public void apply() {
        StorageConfig config = FlowChartState.getInstance().getConfig();
        if (null == config) {
            config = new StorageConfig();
        }
        config.setValue(testValue.getText());
        isModified = false;
//        Messages.showInfoMessage(config.getName(), config.getValue()+"---");
    }

}
